var qcloud = require("../vendor/wafer2-client-sdk/index")
var util = require("../utils/util")
var config = require("../config")

function onSuccess(locker, res)
{
  if(res.data.code != 0)
  {
    util.showModel("参数异常", res.data.msg);
  }
  else
  {
    util.showSuccess(locker, "通讯成功");
    if (res.data.sync != null) {
      //todo 先这样
      var app = getApp()
      if (res.data.sync.credit) {
        console.error(res.data.sync.credit);
        app.globalData.credit = res.data.sync.credit;
      }
      if(res.data.sync.userinfo)
      {
        if(res.data.sync.userinfo.qrcode)
        {
          app.globalData.userInfo.qrcode = res.data.sync.userinfo.qrcode;
        }
      }
      if (res.data.sync.addresses){
        app.globalData.address = res.data.sync.addresses;
      }
      if(res.data.sync.address){
        for(var i = 0; i < app.globalData.address.length; i++)
        {
          if(app.globalData.address[i].id == res.data.sync.address.id)
          {
            app.globalData.address[i] = res.data.sync.address;
            break;
          }
        }
      }
    }
  }
}

function onFail(locker, err)
{
  util.showModel("通讯失败", json.stringify(err));
}

function onComplete(locker)
{
  console.log("通讯完成");
}

function buildPackage(data, url, login, options)
{
  console.log("请求url："+url);


  var locker = util.showBusy("通讯中..");

  if(options == null)
  {
    options = {};
  }

  var ret = 
  {
      url: url,
      login: login || false,
      success: function(res) {
        if(res.data.code == 0)
        {
          onSuccess(locker, res);
          if(options.success)
          {
            options.success(res);
          }
        }
        else
        {
          onFail(locker, res);
          if (options.fail) {
            options.fail(res);
          }
        }
      },
      fail: function(res){
        onFail(locker, res);
        if(options.fail)
        {
          options.fail(res);
        }
      },
      complete:function(res){
        onComplete(locker, res);
        if(options.complete)
        {
          options.complete(res);
        }
      },
      data: data,
  };

  return ret;
}

function get(data)
{
  data.method = "GET";
  qcloud.request(data);
}

function post(data)
{
  data.method = "POST";
  qcloud.request(data);
}

//发布商品
function publish(header, options)
{
  var url = `${config.host}/v1/product/publish`;
  post(buildPackage(header, url, false, options));
}

//查看商品详情
function productDetail(id, options)
{
  var url = `${config.host}/v1/product/${id}/detail`;
  get(buildPackage({}, url, false, options))
}

//请求我的数据
function myInfo(options)
{
  var url = `${config.host}/v1/my/summary`;
  get(buildPackage({}, url, false, options))
}

//获取我发布的列表
function myPublished(pageIndex, options)
{
  var url = `${config.host}/v1/my/published/${pageIndex}`;
  get(buildPackage({}, url, false, options));
}

//我参与的列表
function myAttanded(pageIndex, options)
{
  var url = `${config.host}/v1/my/attanded/${pageIndex}`;
  get(buildPackage({}, url, false, options));
}

//我获得的列表
function myGot(pageIndex, options)
{
  var url = `${config.host}/v1/my/acquired/${pageIndex}`;
  get(buildPackage({}, url, false, options));
}

//购买发布使用次数
function subscribe(id, options)
{
  var url = `${config.host}/v1/service/subscribe/${id}`;
  get(buildPackage({}, url, false, options));
}

//砍一刀
function bargain(id, options)
{
  var url = `${config.host}/v1/product/${id}/bargain`;
  post(buildPackage({id:id}, url, false, options));
}

//获取助力好友列表
function getBargains(id, pageIndex, options)
{
  var url = `${config.host}/v1/${id}/bargains/${pageIndex}`;
  get(buildPackage({}, url, false, options));
}

//添加收货地址
function createAddress(header, options)
{
  var url = `${config.host}/v1/address/create`;
  post(buildPackage(header, url, false, options));
}

//修改收货地址
function editAddress(header, options)
{
  var url = `${config.host}/v1/address/edit`;
  post(buildPackage(header, url, false, options));
}

//删除收货地址
function delAddress(id, options)
{
  var url = `${config.host}/v1/address/del/${id}`;
  get(buildPackage({}, url, false, options));
}

//修改默认地址
function checkAddress(id, options)
{
  var url = `${config.host}/v1/address/check/${id}`;
  get(buildPackage({}, url, false, options));
}

function clearPublishTimes()
{
  var args = 'cleartimes';
  var url = `${config.host}/v1/gmcmd/cmd/${args}`;
  get(buildPackage({}, url, false, {}));
}

function commond(args, options)
{
  var url = `${config.host}/v1/gmcmd/cmd/${args}`;
  get(buildPackage({}, url, false, {}));
}

function getToken(scene, options)
{
  var url = `${config.host}/v1/weapp/getWXACodeUnlimit/${scene}`;
  get(buildPackage({}, url, false, options));
}

function placeOrder(order, options)
{
  var url = `${config.host}/v1/wxpay/placeOrder`;
  post(buildPackage(order, url,false, options));
}

function queryOrder(tradeId, options)
{
  var url = `${config.host}/v1/wxpay/${tradeId}/queryOrder`;
  get(buildPackage({}, url, false, options));
}

function showProducts(page, tag, options) {
  var url = `${config.host}/v1/lobby/${tag}/show/${page}`;
  get(buildPackage({}, url, false, options));
}

function getProductConfig(options){
  var url = `${config.host}/v1/product/getconfig`;
  get(buildPackage({}, url, false, options));
}

function buy(id, options){
  var url = `${config.host}/v1/user/buy`;
  post(buildPackage({id}, url, false, options));
}

function changeQRCode(qrUrl, options)
{
  var url = `${config.host}/v1/user/changeqrcode`;
  post(buildPackage({ url:qrUrl }, url, false, options));
}

function encashment(amount, options)
{
  var url = `${config.host}/v1/wxpay/encashment`;
  post(buildPackage({ amount }, url, false, options));
}

module.exports = 
{
  publish,
  productDetail,
  myInfo,
  myPublished,
  myAttanded,
  myGot,
  subscribe, 
  clearPublishTimes,
  bargain,
  getBargains,
  getToken,
  createAddress,
  editAddress,
  delAddress,
  checkAddress,
  placeOrder,
  showProducts,
  getProductConfig,
  queryOrder,
  buy,
  commond,
  changeQRCode,
  encashment
}